from django.db import models
from frame_server.model import BaseModel

'''角色'''
class Role(BaseModel):
    roleName = models.CharField(max_length=255, db_column="role_name", verbose_name="角色名")
    roleAlias = models.CharField(unique=True, max_length=255, db_column="role_alias", verbose_name="角色别名")
    sort = models.PositiveIntegerField(null=True,blank=True, verbose_name="排序")
    parentId = models.ForeignKey(to='self', db_column="parent_id", db_constraint=False, on_delete=models.CASCADE, null=True, blank=True, verbose_name="父级id")
    
    class Meta:
        db_table = 'system_role'
        indexes = [
            models.Index(fields=['roleName']),
            models.Index(fields=['roleAlias']),
            models.Index(fields=['parentId']),
            models.Index(fields=['roleName','parentId'])
        ]
    
    def __str__(self):
        return self.roleName
    
# 角色多对多关联菜单
class RoleMenu(BaseModel):
    role = models.ForeignKey(Role, db_column="role_id", db_constraint=False, on_delete=models.CASCADE, null=True, blank=True, verbose_name="角色id")
    menu = models.ForeignKey(to="menu.Menu", db_column="menu_id", db_constraint=False, on_delete=models.CASCADE, null=True, blank=True, verbose_name="菜单id")

    def __str__(self):
        return f"{self.role_id}-{self.menu_id}"
    
    class Meta:
        db_table = 'system_role_menu'
        indexes = [
            models.Index(fields=['role']),
            models.Index(fields=['menu']),
            models.Index(fields=['role','menu']),
        ]

# 用户多对多关联角色
class UserRole(BaseModel):
    user = models.ForeignKey(to="user.User", db_column="user_id", db_constraint=False, on_delete=models.CASCADE, null=True,blank=True, verbose_name="用户id")
    role = models.ForeignKey(Role, db_column="role_id", db_constraint=False, on_delete=models.CASCADE, null=True,blank=True, verbose_name="角色id")

    def __str__(self):
        return f"{self.user_id}-{self.role_id}"
    
    class Meta:
        db_table = 'system_user_role'
        indexes = [
            models.Index(fields=['user']),
            models.Index(fields=['role']),
            models.Index(fields=['user','role'])
        ]